package com.wangzhixuan.commons.base;

import java.security.Security;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import com.wangzhixuan.commons.utils.ReadXmlUtil;

import oracle.security.pki.OraclePKIProvider;

public class JdbcDao {
	public static Connection getConnection() {
		try {

			ReadXmlUtil.host="27.115.117.6";
			ReadXmlUtil.protocol = "TCP";
			ReadXmlUtil.service_name = "orcl";
			ReadXmlUtil.user = "FKDBTEST";
			ReadXmlUtil.password = "FKDBTEST";
			ReadXmlUtil.port = "1521";
			ReadXmlUtil.directory = "C:\\\\wallet";

			Class.forName("oracle.jdbc.driver.OracleDriver");
			Security.addProvider(new OraclePKIProvider());
			String e = "jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = " + ReadXmlUtil.protocol
					+ ")(HOST = " + ReadXmlUtil.host + ")(PORT = " + ReadXmlUtil.port + ")))" + "(CONNECT_DATA ="
					+ "(SERVICE_NAME = " + ReadXmlUtil.service_name + ")" + ")" + ")";
			Properties props = new Properties();
			props.put("user", ReadXmlUtil.user);
			props.put("password", ReadXmlUtil.password);
			props.put("oracle.net.wallet_location",
					"(SOURCE=(METHOD=file)(METHOD_DATA=(DIRECTORY=" + ReadXmlUtil.directory + ")))");
			Connection conn = DriverManager.getConnection(e, props);
			return conn;
		} catch (Exception arg2) {
			arg2.printStackTrace();
			throw new RuntimeException(arg2);
		}
	}

	public static void main(String[] args) {
		try {
			getConnection().close();
		} catch (SQLException arg1) {
			arg1.printStackTrace();
		}

	}

	public static void close(Connection conn, PreparedStatement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException arg5) {
				arg5.printStackTrace();
				throw new RuntimeException(arg5);
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException arg4) {
				arg4.printStackTrace();
				throw new RuntimeException(arg4);
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException arg3) {
				arg3.printStackTrace();
				throw new RuntimeException(arg3);
			}
		}

	}

	public static List query(String sql) {
		Connection conn = null;
		PreparedStatement  stmt = null;
		ResultSet rs = null;

		try {
			conn = getConnection();
			stmt =  conn.prepareStatement(sql);
			rs = stmt.executeQuery(sql);
			new ArrayList();
			ResultSetMetaData rsmd = rs.getMetaData();
			int cloumCount = rsmd.getColumnCount();
			ArrayList e = new ArrayList();

			while (rs.next()) {
				HashMap record = new HashMap();

				for (int i = 1; i <= cloumCount; ++i) {
					record.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
				}

				e.add(record);
			}

			ArrayList arg9 = e;
			return arg9;
		} catch (SQLException arg12) {
			arg12.printStackTrace();
		} finally {
			close(conn, stmt, rs);
		}

		return null;
	}
}